package team.yidong.fenda.serviceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.yidong.fenda.entity.Hospital;
import team.yidong.fenda.entity.HospitalField;
import team.yidong.fenda.repository.HospitalFieldRepository;
import team.yidong.fenda.repository.HospitalRepository;
import team.yidong.fenda.repository.QuestionRepository;
import team.yidong.fenda.service.HospitalFieldService;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuanlong on 2017/10/16.
 */
@Service
public class HospitalFieldServiceImpl implements HospitalFieldService {
    @Autowired
    HospitalFieldRepository hospitalFieldRepository;
    @Autowired
    HospitalRepository hospitalRepository;
    @Autowired
    QuestionRepository questionRepository;

    @Override
    public JSONObject findHospitalField(Integer hospitalId, String hospitalName, Integer offset, Integer limit) {
        Pageable pageable = new PageRequest(offset, limit, Sort.Direction.ASC, "id");
        Page<HospitalField> hospitalFieldPage = hospitalFieldRepository.findAll(new Specification<HospitalField>() {
            @Override
            public Predicate toPredicate(Root<HospitalField> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (hospitalId != 1) {
                    Hospital hospital = hospitalRepository.findOne(hospitalId);
                    Predicate p1 = criteriaBuilder.equal(root.get("hospital").as(Hospital.class), hospital);
                    predicateList.add(p1);
                }
                if (hospitalName != null && !hospitalName.equals("")) {
                    String hName = "%" + hospitalName + "%";
                    Hospital hospital = hospitalRepository.findByNameLike(hName);

                    Predicate p = criteriaBuilder.equal(root.get("hospital").as(Hospital.class), hospital);
                    predicateList.add(p);
                }
                criteriaQuery.where(predicateList.toArray(new Predicate[predicateList.size()]));
                return null;
            }
        }, pageable);
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(new String[]{"hospitalPermissionSet", "hospitalImgSet", "hospitalFieldList", "userSet", "masterList"});
        JSONObject jsonObject = JSONObject.fromObject(hospitalFieldPage, jsonConfig);
        String temp = "{\"total\":" + jsonObject.getString("totalElements") + ",\"rows\":" + jsonObject.getString("content") + "}";
        JSONObject j = JSONObject.fromObject(temp);

        return j;
    }

    @Transactional
    @Override
    public void addHospitalField(String hospitaName, String fieldName) {
        Hospital hospital = hospitalRepository.findByName(hospitaName);
        HospitalField hospitalField = new HospitalField();
        hospitalField.setInfo(fieldName);
        hospitalField.setHospital(hospital);
        hospitalFieldRepository.save(hospitalField);
    }

    @Transactional
    @Override
    public void deleteHospitalField(Iterable<HospitalField> hospitalFieldIterable) {
        hospitalFieldRepository.delete(hospitalFieldIterable);
    }

    @Transactional
    @Override
    public void saveHospitalField(HospitalField hospitalField) {

        hospitalFieldRepository.save(hospitalField);
    }

    @Transactional
    @Override
    public void saveHospitalFieldAndQuestionClass(String jsonData, String oldFieldName) {
        JSONObject jsonObject = JSONObject.fromObject(jsonData);
        HospitalField hospitalField = (HospitalField) JSONObject.toBean(jsonObject, HospitalField.class);
        hospitalFieldRepository.save(hospitalField);
        String questionClass = jsonObject.getString("info");

        questionRepository.updateQuestionClass(questionClass, oldFieldName);
    }

    @Override
    public String updateFieldIcon(Integer fieldId, String fileName) {
        HospitalField hospitalField = hospitalFieldRepository.findOne(fieldId);
        hospitalField.setFieldIcon("https://qny.liuxuanlong.xyz/" + fileName);
        hospitalFieldRepository.save(hospitalField);
        return "添加成功";
    }
}
